package com.whisperarts.financer.app.views;

import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.part.ViewPart;

import com.whisperarts.financer.app.actions.ActionsFactory;
import com.whisperarts.financer.app.model.rcp.AccountsContentProvider;
import com.whisperarts.financer.app.model.rcp.AccountsLabelProvider;
import com.whisperarts.financer.model.account.Account;
import com.whisperarts.financer.model.account.AccountsCategory;

public class NavigatorView extends ViewPart {

	public static final String ID = "com.whisperarts.financer.app.views.navigator";

	private static TreeViewer viewer;

	private FormToolkit toolkit;
    private Form form;
    
	@Override
	public void createPartControl(Composite parent) {
		toolkit = new FormToolkit(parent.getDisplay());
		
		form = toolkit.createForm(parent);
        form.setText("Accounts");        
        form.setImage(PlatformUI.getWorkbench().getSharedImages()
				.getImage(ISharedImages.IMG_OBJ_FOLDER));
        
        toolkit.decorateFormHeading(form);
        toolkit.paintBordersFor(form.getBody());
        
        form.getBody().setLayout(parent.getLayout());
        
		viewer = new TreeViewer(form.getBody(), SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.BORDER);
		
		
		viewer.setAutoExpandLevel(2);		
		viewer.setContentProvider(new AccountsContentProvider());
		viewer.setLabelProvider(new AccountsLabelProvider());
		viewer.setInput(new Object());
		viewer.setData(FormToolkit.KEY_DRAW_BORDER,
                FormToolkit.TEXT_BORDER);
		
		getSite().setSelectionProvider(viewer);
		
		viewer.addDoubleClickListener(new IDoubleClickListener() {
			@Override
			public void doubleClick(DoubleClickEvent event) {
				Object selection = ((IStructuredSelection)getViewer().getSelection()).getFirstElement();
				if (selection instanceof Account) {
					ActionsFactory.doOpenAccountAction((Account) selection, getViewSite().getWorkbenchWindow());
				}
				else if (selection instanceof AccountsCategory) {
					toggleExpand(selection);						
				}
				else  {
					
					//TODO do nothing?
				}
			}

			private void toggleExpand(Object selection) {
				if (viewer.getExpandedState(selection)) {
					viewer.collapseToLevel(selection, 2);
				} else {
					viewer.expandToLevel(selection, 1);						
				}
			}
		});
//		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
//			@Override
//			public void selectionChanged(SelectionChangedEvent event) {
//				if(event.getSelection() instanceof IStructuredSelection) {
//					IStructuredSelection selection = (IStructuredSelection)event.getSelection();
//					if (! (selection.getFirstElement() instanceof Account || selection.getFirstElement() instanceof AccountsCategory)) {
//						viewer.removeSelectionChangedListener(this);
//						viewer.setSelection(null, false);
//						viewer.addSelectionChangedListener(this);
//					}
//				}
//			}
//		});
		
	}
	
	
	@Override
	public void setFocus() {
		viewer.getControl().setFocus();
	}	
	
	public static TreeViewer getViewer() {
		return viewer;
	}
	
}
